package com.ruoyi.customer.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customer.domain.CustomerInfo;
import com.ruoyi.customer.service.ICustomerInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.customer.mapper.ContractMapper;
import com.ruoyi.customer.domain.Contract;
import com.ruoyi.customer.service.IContractService;

/**
 * 合同信息Service业务层处理
 *
 * @author yhy
 * @date 2022-11-27
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ICustomerInfoService customerInfoService;

    /**
     * 查询合同信息列表
     *
     * @param contract 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return getBaseMapper().selectContractList(contract);
    }

    /**
     * 将nullify_state状态设置为 作废
     * @param id
     * @return
     */
    @Override
    public int updateByNullifyStatus(Long id) {
        return getBaseMapper().updateByNullifyStatus(id);
    }

    /**
     * 将affix_seal_state设置为 盖章
     * @param id
     * @return
     */
    @Override
    public int updateAffixSealStatus(Long id) {
        return getBaseMapper().updateAffixSealStatus(id);
    }

    /**
     * 将审核状态设置为 审核通过
     * @param id
     * @return
     */
    @Override
    public int updateAuditPassStatus(Long id) {
        return getBaseMapper().updateAuditPassStatus(id);
    }

    /**
     * 将审核状态设置为 审核不通过
     * @param id
     * @return
     */
    @Override
    public int updateAuditOverStatus(Long id) {
        return getBaseMapper().updateAuditOverStatus(id);
    }

    @Override
    public List<Contract> selevtByCustomerId(Long customerId) {
        List<Contract> contractList = getBaseMapper().selectByCustomerId(customerId);
        return contractList;
    }


    // 重写保存方法
    @Override
    public boolean save(Contract entity) {
        // 基于当前用户 id 获取用户对象
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        // 添加录入时间
        entity.setInputTime(new Date());
        // 添加录入人
        entity.setInputUser(sysUser.getUserName());


        // 设置默认状态
        // 添加时默认状态为 未审核
        entity.setAuditState(0);
        // 添加时默认状态为 未作废
        entity.setAffixSealState(0);
        // 添加时默认状态为 未盖章
        entity.setNullifyState(0);

        return super.save(entity);
    }

    // 重写更新方法
    @Override
    public boolean updateById(Contract entity) {
        if (entity.getAuditState() == -1){
            entity.setAuditState(0);
        }
        return super.updateById(entity);
    }
}
